<?php

class CategoriesController extends AppController {
  //public $scaffold;
  public $helpers = array('Html', 'TWSForm', 'Paginator');
  public $components = array('Session');
  public $name = 'Categories';
  public $paginate;

  function __construct($request=null, $response=null) {
    parent::__construct($request, $response);
    $this->paginate = array(
        'limit' => Configure::read('App.pageResultLimit'),
        'order' => array(
            'Category.name' => 'asc'
            ));
  }

  function index() {
    $this->set('title_for_layout','Isle Vow Gallery');
    $this->set("meta_keywords","Photos Images Gallery Pics Pictures Isle Vow, Trinidad and Tobago");
    $this->set("meta_description","A list of photos and image galleries for Isle Vow.");
    
    $sql = "Select Category.id , Category.name, Category.company_id, Count(Album.id) AS AlbumCount ";
    $sql .= "from categories AS Category, albums AS Album ";
    $sql .= "where Album.category_id=Category.id ";
    $sql .= "and Album.id in ( ";
    $sql .= "	Select distinct(Album.id)  ";
    $sql .= "	from albums as Album, photos as Photo ";
    $sql .= "	where Album.id=Photo.album_id ";
    $sql .= "	group by Album.id ";
    $sql .= "	having count(Photo.id) >0 ";
    $sql .= ") ";
    $sql .= "group by Category.id ";
    $sql .= "order by Category.name ";
    $result = $this->Category->query($sql);
    $this->set('categories', $result);

    $sql = "Select Company.id, Company.name, Count(Video.id) as VideoCount ";
    $sql .= "from videos as Video inner join companies as Company on Video.company_id = Company.id ";
    $sql .= "where Company.active ='Y' ";
    $sql .= "group by Company.id ";
    $sql .= "having VideoCount >0 ";
    $sql .= "order by Company.name ";
    $this->loadModel('Video');
    $result = $this->Video->query($sql);
    $this->set('videos', $result);
  }

  //Points to menu page
  function admin_main() {
    $this->layout = "admin";
    $this->set('title_for_layout','Gallery');
  }

//admin and public version
  function admin_index() {
    $this->layout = "admin";
    $this->set('categories', $this->paginate('Category'));
  }

  //public version
  function view($id=null) {
    $this->Category->id = $id;
    $this->set('categories', $this->Category->read());
    //TO-DO set the view here to the same as viewing all the announcements
  }

  //admin
  function admin_add() {
    $this->layout = "admin";
    $test = $this->Category->validate;
    if ($this->request->is('post')) {
      $this->_processAction($this->request, 'add');
    }
  }

  //admin
  function admin_edit($id = null) {
    $this->layout = "admin";
    $this->Category->id = $id;
    if ($this->request->is('get')) {
      $this->request->data = $this->Category->read();
    } else {
      //check for delete announcement
      if (isset($this->request->data['deleteBtn'])) {
        $this->admin_delete($id);
      } else {
        $this->_processAction($this->request, 'edit', $id);
      }
    }
  }

  private function _processAction($dataIn, $action='add', $id=null) {
      $data = $this->_processRequestData($dataIn);
      $this->Category->set($data);
      if ($this->Category->validates()) {
        if ($this->Category->save($data, false)) {
          $this->Session->setFlash('The record was successfully saved.');
          $this->redirect(array('action' => 'index'));
        }
      } else {
        if($action!=='add') {
          $data['Category']['id']=$id;
        }
        $this->request->data['Category'] = $data['Category'];
        $this->set('errors', $this->Category->validationErrors);
      }
      if($action==='add') {
        $this->render('admin_add');
      } else {
        $this->render('admin_edit');
      }
  }
  
  function admin_delete($id) {
    $this->layout = "admin";
    //http://book.cakephp.org/2.0/en/models/deleting-data.html
    if ($this->Category->delete($id, true)) {
      $this->Session->setFlash('The record has been deleted.');
      $this->redirect(array('action' => 'index'));
    }
  }

}

?>
